package com.njtoyo.taxi.admin.security;

import com.google.common.base.Strings;
import com.njtoyo.taxi.admin.library.jwt.JwtHelper;
import com.njtoyo.taxi.admin.rest.wrapper.Identity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * @Author Dell
 * @Date 2021/6/9 17:15
 */
@Component
public class TokenVerifier {

    @Autowired
    private AdminTokenParser adminTokenParser;

    @Value("${config.token.apiKey}")
    private String apiKey;

    public Identity verifyIdentityToken(String token) {


        if(JwtHelper.isExpire(token,apiKey)){
            Identity identity = new Identity();
            identity.setIsExpired(true);
            return identity;
        }

        String parsed = JwtHelper.parse(token,apiKey);

        if(Strings.isNullOrEmpty(parsed)){
            return null;
        }

        return adminTokenParser.parseToken(parsed);

    }

}
